package com.bgs.eat.chat.mapper;

import com.bgs.eat.chat.entity.ChatRecordEntity;
import com.bgs.eat.config.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChatRecordMapper extends BaseMapper<ChatRecordEntity> {

    /**
     * @Author G胖
     * @Description 查询商家给那些用户发送了消息
     * @Date 2022-10-24 下午 01:40
     * @Return com.bgs.eat.config.CommonResult

     */
    @Select("select * from chat_record where id in(select max(id) from chat_record GROUP BY to_id) and to_id !=#{toId}  and from_id =#{toId} ORDER BY id desc")
    @Results(
            {
                    @Result(property = "msgType", column = "msg_type"),
                    @Result(property = "avatarId", column = "avatar_id"),
                    @Result(property = "nickName",column = "nick_name"),
                    @Result(property = "fromId",column = "from_id"),
                    @Result(property = "toId",column = "to_id"),
            }
    )
    List<ChatRecordEntity> getUserChatRecord(@Param("toId") Integer toId);

    /**
     * @Author G胖
     * @Description 查询用户或者商家是否给对方发过消息用来判断最新的历史消息
     * @Date 2022-10-24 下午 01:40
     * @Return com.bgs.eat.config.CommonResult

     */
    @Select("select * from chat_record where from_id=#{toId} and to_id =#{fromId} order by id desc limit 1")
    @Results(
            {
                    @Result(property = "msgType", column = "msg_type"),
                    @Result(property = "avatarId", column = "avatar_id"),
                    @Result(property = "nickName",column = "nick_name"),
                    @Result(property = "fromId",column = "from_id"),
                    @Result(property = "toId",column = "to_id"),
            }
    )
    ChatRecordEntity getUserToChatRecord(@Param("toId") Integer toId,@Param("fromId") Integer fromId);

    /**
     * @Author G胖
     * @Description 查询用户给哪些商家发送了消息
     * @Date 2022-10-25 上午 08:55
     * @Return com.bgs.eat.config.CommonResult

     */
    @Select("select * from chat_record where id in(select max(id) from chat_record GROUP BY to_id) and to_id !=#{toId}  and from_id =#{toId} ORDER BY id desc")
    @Results(
            {
                    @Result(property = "msgType", column = "msg_type"),
                    @Result(property = "avatarId", column = "avatar_id"),
                    @Result(property = "nickName",column = "nick_name"),
                    @Result(property = "fromId",column = "from_id"),
                    @Result(property = "toId",column = "to_id"),
            }
    )
    List<ChatRecordEntity> getBusinessChatRecord(@Param("toId") Integer toId);

    /**
     * @Author G胖
     * @Description 查询用户或者商家是否给对方发过消息用来判断最新的历史消息
     * @Date 2022-10-25 下午 09:00
     * @Return com.bgs.eat.config.CommonResult

     */
    @Select("select * from chat_record where from_id=#{toId} and to_id =#{fromId} order by id desc limit 1")
    @Results(
            {
                    @Result(property = "msgType", column = "msg_type"),
                    @Result(property = "avatarId", column = "avatar_id"),
                    @Result(property = "nickName",column = "nick_name"),
                    @Result(property = "fromId",column = "from_id"),
                    @Result(property = "toId",column = "to_id"),
            }
    )
    ChatRecordEntity getBusinessToChatRecord(@Param("toId") Integer toId,@Param("fromId") Integer fromId);
}
